package com.work.rcj.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.work.rcj.common.base.BaseController;
import com.work.rcj.common.exception.BusinessException;
import com.work.rcj.common.model.Response;
import com.work.rcj.common.model.page.PageResult;
import com.work.rcj.entity.OldPeople;
import com.work.rcj.entity.User;
import com.work.rcj.mapper.UserMapper;
import com.work.rcj.pojo.dto.LoginDto;
import com.work.rcj.pojo.dto.UserDto;
import com.work.rcj.pojo.vo.LoginVo;
import com.work.rcj.pojo.vo.VisitVo;
import com.work.rcj.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 *  用户相关
 *
 * @author rcj
 * @since 2022-02-26
 */
@RestController
@RequestMapping("/user")
public class UserController extends BaseController {

    @Autowired
    private UserService userService;

    /**
     * 登录
     *
     * @param user
     * @return
     */
    @PostMapping("/login")
    public Response<User> login(@RequestBody User user){
        return userService.login(user);
    }



    /**
     * 获取用户信息
     *
     * @param user
     * @return
     */
    @PostMapping("/getUserInfo")
    public Response<User> getUserInfo(@RequestBody User user){
        return Response.success(userService.getById(user.getId()));
    }


    /**
     * 修改用户资料
     *
     * @param user
     * @return
     */
    @PostMapping("/updateUserInfo")
    public Response updateUserInfo(@RequestBody User user){
        return userService.updateUserInfo(user);
    }


    /**
     * 用户列表
     * @param dto
     * @return
     */
    @PostMapping("/list")
    public Response<PageResult<User>> userList(@RequestBody UserDto dto){
        return Response.success(getDataTable(userService.selectList(dto)));
    }


    /**
     * 新增老年人
     *
     * @param req
     * @return
     */
    @PostMapping("/save")
    public Response save(@RequestBody User req) {
        userService.save(req);
        return Response.success();
    }

    /**
     * 修改用户
     *
     * @param req
     * @return
     */
    @PostMapping("/update")
    public Response update(@RequestBody User req) {
        UpdateWrapper<User> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", req.getId());
        userService.update(req, updateWrapper);
        return Response.success();
    }

    /**
     * 删除用户
     *
     * @param req
     * @return
     */
    @PostMapping("/del")
    public Response del(@RequestBody User req) {
        User entity = new User();
        entity.setId(req.getId());
        entity.setDeleted("1");
        userService.updateById(entity);
        return Response.success();
    }

}

